﻿import java.util.regex.*;
import java.io.*;
//========================================================================================//
public class compiler00001
{
	public static void main(String[] args){
		Pattern identifier =Pattern.compile("[\\s]{0,}[_|.]{0,}[0-9]{0,}[A-Z][\\s|A-Z|0-9|_|.]{0,}[\\s]{0,}",Pattern.CASE_INSENSITIVE);//英文字母A-Z但忽略大小寫
		Pattern number =Pattern.compile("[\\s]{0,}[_|.]{0,}[0-9]{0,}[A-Z][\\s|A-Z|0-9|_|.]{0,}[\\s]{0,}|[0-9|.|\\s]{1,}",Pattern.CASE_INSENSITIVE);//抓取數字有關
		Pattern op =Pattern.compile("[+|-|*|/|;|=|(|)]");
		Pattern mark =Pattern.compile("[;|=|(|)]");
		Pattern iderror =Pattern.compile("[\\s]{0,}[A-Z][A-Z|0-9]{0,}[\\s]{0,}|[\\s]{0,}[A-Z][A-Z|0-9]{0,}[_]{1,}[A-Z|0-9]{1,}[\\s]{0,}",Pattern.CASE_INSENSITIVE);//判斷ID符合
		Pattern numerror =Pattern.compile("[\\s]{0,}[0][\\s]{0,}|[\\s]{0,}[1-9][0-9]{0,}[\\s]{0,}|[\\s]{0,}[0-9]{1,}[.][0-9]{1,}[\\s]{0,}",Pattern.CASE_INSENSITIVE);//判斷數字
		try{
			FileReader fin = new FileReader("d:/test.txt");//讀取來源
			BufferedReader fr = new BufferedReader(fin);
 			//FileOutputStream file = new FileOutputStream("d:/compilerwork/test02.txt");//檔案輸出
			File fil = new File("d:/Compiler.txt");
			PrintWriter file = new PrintWriter("d:/Compiler.txt"); //檔案輸出
			String[] stringArray = new String [100];
			String data;
			while ((data = fr.readLine())!= null)
			{
				String output[] = new String[100];
				int i=0;
				System.out.println("載入:"+data);//一次讀取一行
				//file.println("載入:"+data);//一次讀取一行
				char[] charArray = data.toCharArray();//將字串轉成字元陣列
				Matcher id = identifier.matcher(data); //字串才可比對
				Matcher numc = number.matcher(data);
				Matcher opc = op.matcher(data); 
				while(id.find()){
					String outid="" ;
					for(int a = id.start();a<=id.end()-1;a++){
					outid=outid+charArray[a];
					i = a;
					}//for
					Matcher errorc = iderror.matcher(outid);
					if(errorc.matches()==true){
					output[i] = outid+" "+"id";
					//System.out.println(outid+" "+"id");
					}else{
					output[i] = outid+" "+"error";
					//System.out.println(outid+" "+"error");
					}//if else				
				}//while id
				while(numc.find()){
					String outnum="";
					for(int a = numc.start();a<=numc.end()-1;a++){
					outnum=outnum+charArray[a];
					i = a;
					}//for
					Matcher errorc = numerror.matcher(outnum);
					Matcher errord = identifier.matcher(outnum);//判斷是否出現跟ID有關的數字
				if(errord.matches()==false){
					if(errorc.matches()==true){
					output[i] = outnum+"  "+"num";
					//System.out.println(outnum+"  "+"num");
					}else{
					output[i] = outnum+"  "+"error";
					//System.out.println(outnum+"  "+"error");
					}//if else
				 }//if
				}//while numc
				while(opc.find()){
					String outop="";
					for(int a = opc.start();a<=opc.end()-1;a++){
					outop=outop+charArray[a];
					i = a;
					}//for
					Matcher opamk = mark.matcher(outop);//判斷是否跟mark相同
					if(opamk.matches()==true){
					output[i] = outop +" "+ outop;
					//System.out.println(outop+"  "+outop);
					}else{
					output[i] = outop+"  "+"op";
					//System.out.println(outop+"  "+"op");
					}//if else
				}//while opc
				for(int x = 0 ; x<= data.length();x++){
					if(output[x]!=null){
					System.out.println(output[x]+" " );//輸出
					file.println(output[x]+" " );//寫入輸出
					}
				}
			}//while			
			file.flush(); 
			fin.close();
		}//try
		/*//如果不捕捉 會出錯 因為本身FileInputStream會丟出FileNotFoundException
		catch(FileNotFoundException ee){
			System.out.println("File Not Found");
		}//catch*/
		//如果不捕捉 會出錯 因為本身read,close會丟出IOException
		catch(IOException ie){
			System.out.println("I/O ERROR");
		}//catch
	}//main
}